package com.example.module_home

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.example.lib_base.view.BaseFragment
import com.example.lib_common.view.view.BaseMvvmFragment
import com.example.module_home.adapter.AnnouncementAdapter
import com.example.module_home.databinding.FragmentHomeBinding
import com.example.module_home.vm.HomeViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HomeFragment : BaseMvvmFragment<FragmentHomeBinding>() {
    //在Fragment 中 使用activityViewModels() 在activity 中使用ViewModels()
    private val mViewModel : HomeViewModel by activityViewModels()

    override fun getLayoutId(): Int =R.layout.fragment_home
    private var pageIndex=1
    private val pageSize =10
    val noticeAdapter = AnnouncementAdapter()



    override fun initViews() {
        super.initViews()
        mDataBinding.rv.layoutManager=LinearLayoutManager(requireContext())
        mDataBinding.rv.adapter=noticeAdapter


    }
    override fun loadData() {
       mViewModel.loadNoticeData(pageIndex,pageSize)
    }

    override fun observeDataChanged() {
        mViewModel.mSuccess.observe(requireActivity()){
            Log.i("HomePage", "=========> ${GsonUtils.toJson(it)}")
            noticeAdapter.data.addAll(it)
            noticeAdapter.notifyDataSetChanged()
        }
    }
}